package com.cheche.dn.infrastructure.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Config - Redis
 * Created by cheshun on 14-8-13.
 */
@Configuration
public class RedisConfig {

	@Value("${redis.pool.maxTotal}")
	private int maxTotal;
	@Value("${redis.pool.maxIdle}")
	private int maxIdle;
	@Value("${redis.pool.minIdle}")
	private int minIdle;
	@Value("${redis.pool.minEvictableIdleTime}")
	private long minEvictableIdleTime;
	@Value("${redis.pool.maxWait}")
	private long maxWait;
	@Value("${redis.pool.testOnBorrow}")
	private boolean testOnBorrow;
	@Value("${redis.pool.testOnReturn}")
	private boolean testOnReturn;

	public JedisPoolConfig jedisPoolConfig() {
		JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
		jedisPoolConfig.setMaxTotal(maxTotal);
		jedisPoolConfig.setMaxIdle(maxIdle);
		jedisPoolConfig.setMinIdle(minIdle);
		jedisPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTime);
		jedisPoolConfig.setMaxWaitMillis(maxWait);
		jedisPoolConfig.setTestOnBorrow(testOnBorrow);
		jedisPoolConfig.setTestOnReturn(testOnReturn);
		return jedisPoolConfig;
	}

	@Value("${redis.host}")
	public String host;
	@Value("${redis.port}")
	public int port;
	@Value("${redis.password}")
	private String password;
	@Value("${redis.db.token}")
	private int tokenDb;
	@Value("${redis.db.common}")
	private int commonDb;

	@Bean(name = "tokenRedis")
	public RedisTemplate<String, Object> tokenRedis() {
		return redisTemplate(connectionFactory(host, port, password, tokenDb));
	}

	@Bean(name = "commonRedis")
	public RedisTemplate<String, String> commonRedis() {
		return new StringRedisTemplate(connectionFactory(host, port, password, commonDb));
	}

	public JedisConnectionFactory connectionFactory(String host, int port, String password, int db) {
		JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig());
		jedisConnectionFactory.setHostName(host);
		jedisConnectionFactory.setPort(port);
		jedisConnectionFactory.setPassword(password);
		jedisConnectionFactory.setDatabase(db);
		jedisConnectionFactory.afterPropertiesSet();
		return jedisConnectionFactory;
	}

	public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory connectionFactory) {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(connectionFactory);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}
